<template>
  <div class="forget-pw-back">
    <div class="na-bg-img">
      <div class='log-in-back' >
        <div @click="backClick">
          <van-icon name="arrow-left" size="30" color="white"/>
        </div>
      </div>
      <div class="forget-pw-context">
        <div><span>找回密码</span></div>
        <div><van-field v-model="email" label="邮箱" placeholder="请输入邮箱" /></div>
        <div><van-button type="info" @click="sendCoodClick" :disabled="isDisabled">发送验证码</van-button></div>
        <div><van-field v-model="code" label="验证码" placeholder="请输入验证码" /></div>
        <div><van-field v-model="newPassword" type="password" label="新密码" /></div>
        <div><van-field v-model="confirmNewPassword" type="password" label="确认新密码" @input="confirmNewPasswordEvent" /></div>
        <div><span v-show="confirmNewPasswordCommon">密码不一致</span></div>
        <div><van-button type="info" @click="confirmClick" :disabled="confirmIsDisabled">确认更改密码</van-button></div>
      </div>
    
      
    </div>
  </div>
</template>
<script>
import Vue from 'vue';
import axios from 'axios'

import networkPath from '../../network/networkPath'

import { Button, Toast } from 'vant';
import { Field } from 'vant';

Vue.use(Field);
Vue.use(Button);

export default {
  name: 'ForgetPassword',
  data() {
    return {
      email: '',
      code: '',
      newPassword: '',
      confirmNewPassword: '',
      confirmNewPasswordCommon:false,
      isDisabled: false,
      confirmIsDisabled: false
    }
  },
  methods: {
    backClick(){
      this.$router.back()
    },
    confirmNewPasswordEvent(){
      this.confirmNewPasswordCommon = true
      if (this.newPassword == this.confirmNewPassword) {
        this.confirmNewPasswordCommon = false
      }
    },
    sendCoodClick(){
    this.isDisabled = true
    axios.post(`${networkPath.baseURL}/api/user/sendRenewPwd`,`email=${this.email}`).then(res =>{
      if (res.data.code==302) {
        Toast({message:res.data.message ,position: 'top'})
      setTimeout(() => {
      this.isDisabled = false
      }, 60000);
      }else{
        Toast({message:res.data.message ,position: 'top'})
        setTimeout(() => {
          this.isDisabled = false
        }, 3000);
      }
    })
    },
    confirmClick(){
      axios.post(`${networkPath.baseURL}/api/user/retrieve`,
      `email=${this.email}&code=${this.code}&password=${this.newPassword}`).then(res =>{
        console.log(res)
        if (res.data.code==300) {
          Toast({message:res.data.message+'你的新密码为'+this.newPassword,position: 'top'})
          this.$router.push({path: '/login'})
        }else{
          Toast({message:res.data.message ,position: 'top'})
        }
      })
    }

  },
  
}
</script>
<style>

.na-bg-img{
  height: 80vh;
  background-image: linear-gradient(to bottom, #3e59ec, #b4ddf5);
}
.na-bg-img div{
  padding-bottom: 5px;
}
.log-in-back{
  width: 100%;
  height: 44px;
}
.log-in-back div{
  width: 44px;
  position: absolute;
  height: 44px;
  padding-top: 5px;
  padding-left: 0px;
}
.forget-pw-context{
  height: 70vh;
  width: 90vw;
  background-color: rgba(255, 255, 255, 0.63);
  text-align: center;
  margin-left: 5vw;
  line-height: 8vh;
}
</style>